<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>角色列表</title>
        <header th:replace="sys/head :: html"></header>
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="rolename"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" id="search" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn" onclick="xadmin.open('添加角色', appPath + '/sys/role/toAdd', 600, 400)"><i class="layui-icon"></i>添加</button>
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                        </div>
                        <div class="layui-card-body ">
                            <table id="dataTable" lay-filter="table"></table>
                            <script type="text/html" id="toolbar">
                                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
        var table;
        layui.use('table', function(){
            table = layui.table;
            var form = layui.form;
            //第一个实例
            table.render({
                elem: '#dataTable'
                ,height: 'full-160'
                ,url: appPath + '/sys/role/list' //数据接口
                ,page: true //开启分页
                ,cols: [
                    [ //表头
                        {type:'checkbox', fixed: 'left'}
                        ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                        ,{field: 'name', title: '角色名', width:80}
                        ,{field: 'description', title: '描述', width:100}
                        ,{field: 'createTime', title: '创建时间'}
                        ,{field: 'updateTime', title: '更新时间'}
                        ,{fixed: 'right', title: '操作', toolbar: '#toolbar', width: 150}
                    ]
                ]
            });
            //监听提交
            form.on('submit(search)', function(data){
                var rolename = data.field.rolename;
                table.render({
                    elem: '#dataTable'
                    ,height: 'full-160'
                    ,url: appPath + '/sys/role/listByMap' //数据接口
                    ,where:data.field
                    ,page: true //开启分页
                    ,cols: [
                        [ //表头
                            {type:'checkbox', fixed: 'left'}
                            ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                            ,{field: 'name', title: '角色名', width:80}
                            ,{field: 'desc', title: '描述', width:100}
                            ,{field: 'createTime', title: '创建时间'}
                            ,{field: 'updateTime', title: '更新时间'}
                            ,{fixed: 'right', title: '操作', toolbar: '#toolbar', width: 150}
                        ]
                    ]
                });
                return false;
            });
            //监听工具条
            table.on('tool(table)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.ajax({
                            url: appPath + '/sys/role/del',
                            type: 'get',
                            data: {id:data.id},
                            dataType: 'json',
                            success: function (result) {
                                if(result.code == 200){
                                    layer.alert("删除成功", {
                                            icon: 1
                                        },
                                        function() {
                                            //关闭当前frame
                                            xadmin.close();
                                            // 可以对父窗口进行刷新
                                            xadmin.father_reload();
                                            //table.reload("dataTable");
                                        });
                                }else{
                                    layer.alert(result.msg);
                                }
                            }
                        })
                    });
                } else if(obj.event === 'edit'){
                    xadmin.open('编辑角色',appPath + '/sys/role/toEdit?id='+data.id), 600, 400;
                }
            });

        });
        function delAll (argument) {

            //获取选中状态
            var checkStatus = table.checkStatus('dataTable');
            //获取选中数量
            var selectCount = checkStatus.data.length;
            if(selectCount == 0){
                layer.msg('批量删除至少选中一项数据',function(){});
                return false;
            }
            layer.confirm('真的要删除选中的项吗？',function(index){
                layer.close(index);
                index = layer.load(1, {shade: [0.1,'#fff']});

                var isStr="";
                for(var i=0; i<selectCount; i++){
                    isStr = isStr + "," + checkStatus.data[i].id;
                }
                isStr = isStr.substring(1);
                $.ajax({
                    type:'post',
                    data:{"ids":isStr},
                    url:appPath + '/sys/role/delAll',
                    success:function(data){
                        if(data.code==200){
                            $('#search').click();
                        }
                        layer.close(index);
                        layer.msg(data.msg);
                    },error:function(code){
                        parent.layer.msg('操作失败!');
                    }
                });
            })
        }
    </script>
</html>